package org.dromara.Nyt7FarmerInsuranceInfo.controller;

import java.util.Collections;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.Nyt7FarmerInsuranceInfo;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.Nyt7FarmerInsuranceInfoAll;
import org.dromara.Nyt7InsuranceProduct.domain.Nyt7InsuranceProduct;
import org.dromara.system.domain.SysPost;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.vo.Nyt7FarmerInsuranceInfoVo;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.bo.Nyt7FarmerInsuranceInfoBo;
import org.dromara.Nyt7FarmerInsuranceInfo.service.INyt7FarmerInsuranceInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 农户投保信息
 *
 * @author Lion Li
 * @date 2024-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/Nyt7FarmerInsuranceInfo/Nyt7FarmerInsuranceInfo")
public class Nyt7FarmerInsuranceInfoController extends BaseController {

    private final INyt7FarmerInsuranceInfoService nyt7FarmerInsuranceInfoService;

    /**
     * 查询农户投保信息列表
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:list")
    @GetMapping("/list")
    public TableDataInfo<Nyt7FarmerInsuranceInfoVo> list(Nyt7FarmerInsuranceInfoBo bo, PageQuery pageQuery) {
        return nyt7FarmerInsuranceInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出农户投保信息列表
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:export")
    @Log(title = "农户投保信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Nyt7FarmerInsuranceInfoBo bo, HttpServletResponse response) {
        List<Nyt7FarmerInsuranceInfoVo> list = nyt7FarmerInsuranceInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "农户投保信息", Nyt7FarmerInsuranceInfoVo.class, response);
    }

    /**
     * 获取农户投保信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:query")
    @GetMapping("/{id}")
    public R<Nyt7FarmerInsuranceInfoAll> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(nyt7FarmerInsuranceInfoService.queryById(id));
    }

    /**
     * 新增农户投保信息
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:add")
    @Log(title = "农户投保信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Nyt7FarmerInsuranceInfoBo bo) {
        return toAjax(nyt7FarmerInsuranceInfoService.insertByBo(bo));
    }

    /**
     * 修改农户投保信息
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:edit")
    @Log(title = "农户投保信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Nyt7FarmerInsuranceInfoBo bo) {
        return toAjax(nyt7FarmerInsuranceInfoService.updateByBo(bo));
    }

    /**
     * 删除农户投保信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:remove")
    @Log(title = "农户投保信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(nyt7FarmerInsuranceInfoService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 获取农户数
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:list")
    @GetMapping("/count")
    public R<Long> getFarmerCount() {
        Long count = nyt7FarmerInsuranceInfoService.getFarmerCount();
        return R.ok(count);
    }
    /**
     * 农户信息导入
     */
    @Log(title = "农户信息导入", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Boolean> excelImport(MultipartFile file) throws Exception {
        // 使用静态方法导入Excel文件
        List<Nyt7FarmerInsuranceInfoAll> list = ExcelUtil.importExcel(file.getInputStream(), Nyt7FarmerInsuranceInfoAll.class);

        // 处理导入的数据（保存到数据库）

        return R.ok( nyt7FarmerInsuranceInfoService.saveBatch(list));
    }

    /**
     * 导出选中的农户投保信息列表
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:export")
    @Log(title = "农户投保信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelected")
    public void exportSelected(@RequestBody List<Long> ids, HttpServletResponse response) {
        List<Nyt7FarmerInsuranceInfoVo> list = nyt7FarmerInsuranceInfoService.queryListByIds(ids);
        ExcelUtil.exportExcel(list, "农户投保信息", Nyt7FarmerInsuranceInfoVo.class, response);
    }


    /**
     * 导入模版下载
     */
    @SaCheckPermission("Nyt7FarmerInsuranceInfo:Nyt7FarmerInsuranceInfo:export")
    @Log(title = "农户投保信息", businessType = BusinessType.EXPORT)
    @PostMapping("/template")
    public void template(HttpServletResponse httpServletResponse){
        List<Nyt7FarmerInsuranceInfoAll> emptyList = Collections.emptyList();
        ExcelUtil.exportExcel(emptyList,"农户投保信息-导入模版", Nyt7FarmerInsuranceInfoAll.class,httpServletResponse);
    }


}
